// pages/address_info/address_info.js
var publicFun = require('../../../../utils/publicFun.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    region: [],
    consignee: '',
    phone: '',
    address: '',
    currentId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      //更新当前的titleName
      wx.setNavigationBarTitle({
        title: '编辑收货地址'
      })
      //请求获取当前数据详情
      app.Ajax({
        url: '/api/index.php?app=address&act=edit',
        data: { addr_id: options.id },
        success: (res) => {
          let regionArr = res.region_name.split(' ');
          this.setData({
            consignee:res.consignee,
            address:res.address,
            phone: res.phone_mob,
            region: regionArr
          })
        },
        fail: (err) => {
          console.log(err);
        }
      })
      this.setData({
        currentId: options.id
      })
    } else {
      //更新当前的titleName
      wx.setNavigationBarTitle({
        title: '新增收货地址'
      })
    }
  },

  /*
  * 生命周期函数--监听页面显示
  */
  onShow: function (options) {

  },

  //保存收货地址
  saveAddress: function (e) {
    var reqArr = [
      ['consignee', '请输入收货人地址'],
      ['phone', '请输入联系电话'],
      ['address', '请输入详细地址']
    ];
    var validation = publicFun.requiredFun(reqArr, this.data);
    if (validation.state) {

      if (!this.data.region.length) {
        this.toastShow('请选择所在地区');
        return
      }

      //正则判断手机号码是否合法
      if (!publicFun.regularPhone(this.data.phone)) {
        this.toastShow('请输入合法的电话号码');
        return
      }

      let formData = {
        province: this.data.region[0],
        city: this.data.region[1],
        region: this.data.region[2],
        consignee: this.data.consignee,
        address: this.data.address,
        phone: this.data.phone
      };

      let url = '';
      if(this.data.currentId){
        url = '/api/index.php?app=address&act=edit';
        formData['addr_id'] = this.data.currentId;
      }else{
        url = '/api/index.php?app=address&act=add';
      }
      //发起请求
      app.Ajax({
        method: 'POST',
        url: url,
        data: formData,
        success: (res) => {
          //请求成功
          this.toastShow('保存成功');
          //2秒后返回到上一页
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        },
        fail: (err) => {
          console.log(err);
          this.toastShow(err);
        }
      })

    } else {
      this.toastShow(validation.msg);
      return
    }

  },

  /**
   * 选择所在地区
   */
  bindRegionChange: function (e) {
    var region = e.detail.value;
    this.setData({
      region: region
    })
  },

  /**
  * 显示报错信息
  */
  toastShow: function (message) {
    this.setData({
      message: message,
    })
  },


  /*
  * 绑定收货人信息
  */
  bindConsigneeInput: function (e) {
    this.setData({
      consignee: e.detail.value
    })
  },


  /*
  * 绑定收货人联系电话
  */
  bindPhoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  /*
  * 绑定详细地址信息
  */
  bindAddressInput: function (e) {
    this.setData({
      address: e.detail.value
    })
  },

})